/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cw.view;

import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import org.cw.bean.Establishment;

/**
 *
 * @author SVN - Team
 */
public class FromSearchName extends Form implements CommandListener, ItemStateListener {

    private static final String TITLE = "Search a Establishment";
    private MReviewer curDisplay;
    private TextField txtSearch;
    private ChoiceGroup listEsts;
    private Command cmdSearch, cmdBack, cmdView;
    private FromListEstablishments frmListEst;
    private FromViewEstablistment frmViewEst;
    private Alert err;

    public FromSearchName(MReviewer curDisplay) {
        super(TITLE);
        this.curDisplay = curDisplay;
        init();
    }

    private void init() {
        txtSearch = new TextField("Search ", "", 50, TextField.ANY);
        append(txtSearch);
        listEsts = new ChoiceGroup("", Choice.EXCLUSIVE);
        append(listEsts);
        cmdBack = new Command("Back", Command.BACK, 0);
        cmdSearch = new Command("Search", Command.OK, 1);
        cmdView = new Command("View", Command.OK, 2);
        addCommand(cmdBack);
        addCommand(cmdSearch);
        addCommand(cmdView);
        setCommandListener(this);
        setItemStateListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdSearch) {
            String strNameEst = txtSearch.getString();
            if (!strNameEst.equals("")) {
                searchByName(strNameEst);
            }
        } else if (c == cmdBack) {
            frmListEst = new FromListEstablishments(curDisplay);
            Display.getDisplay(curDisplay).setCurrent(frmListEst);
        } else if (c == cmdView) {

            String strNameEst = listEsts.getString(listEsts.getSelectedIndex());
            Establishment est = getEstablishmentByName(strNameEst);
            if (est != null) {
                frmViewEst = new FromViewEstablistment(curDisplay);
                Display.getDisplay(curDisplay).setCurrent(frmViewEst);
                frmViewEst.setEstablishment(est);
            } else {
                err = new Alert("Error", "null data, please try again", null, AlertType.ERROR);
                Display.getDisplay(curDisplay).setCurrent(err);
            }
        }
    }

    private void searchByName(String nameEst) {
        listEsts.deleteAll();
        Vector listEst = MReviewer.dataStore;
        for (int i = 0; i < listEst.size(); i++) {
            Establishment est = (Establishment) listEst.elementAt(i);
            if (est.getName().toLowerCase().startsWith(nameEst.toLowerCase())) {
                listEsts.append(est.getName(), null);
            }
        }
    }

    private Establishment getEstablishmentByName(String strName) {
        Vector listEst = MReviewer.dataStore;
        for (int i = 0; i < listEst.size(); i++) {
            Establishment est = (Establishment) listEst.elementAt(i);
            if (est.getName().equals(strName)) {
                return est;
            }
        }
        return null;
    }

    public void itemStateChanged(Item item) {
        if (item.equals(txtSearch)) {
            String strNameEst = txtSearch.getString();
            if (!strNameEst.equals("")) {
                searchByName(strNameEst);
            }
        }
    }
}
